package com.jinsi.util;

import com.jinsi.service.tang.iml.SensitiveCharacterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 过滤敏感词
 * @author 唐文博
 * @Date 2022-10-14
 */
@Component
public class Sensitive {
    @Autowired
    private SensitiveCharacterServiceImpl sensitiveCharacterService;

    /**
     * 添加调用自动审核方法
     * @param body
     * areturn
     */
    public Map<String, Object> getSensitiveCharacter(String body) {
        Map<String, Object> map = new HashMap<>();
        //获取到所有敏感词
        List<String> strings = sensitiveCharacterService.getSensitiveCharacter();
        List<String> bannedlist = new ArrayList<>();
        //判断body中有的敏感词―加入集合没有直接退出
        for (String ss : strings) {
            if (body.indexOf(ss) == -1) {
                map.put("body",body);
                map.put("state",1);
                map.put("cause","没有敏感词");
            } else {
                bannedlist.add(ss);
            }
        }

        //对body中敏感词集合进行遍历每个敏感词替换成*返回给map集合
        String boby1 = "";
        for (int i = 0; i < bannedlist.size();i++) {
            String b = "";
            for (int j = 0; j < bannedlist.get(i).length();j++) {
                b += "*";
            }
            boby1 = body.replace(bannedlist.get(i),b);
            body = boby1;
            b = "";
        }
        map.put("body",boby1);
        map.put("state",2);
        map.put("cause","有敏感词");
        return map;
    }
}